<?
##################################
# Allg. Komponenten (Admin)
#
# FileUploadForm($current_file,$current_varname,$new_varname,$file_dir,$size=60)
# PicUploadForm($current_file,$oldname,$newname,$path_to_pics,$size=60)
# selectfromquery($name,$query,$key,$noseltext=false,$onchange='',$size=1)
# selectfromarray($name,$values,$key,$onchange=false)
# datePicker($N,$Value,$optional=false,$mindate=MIN_DATE,$maxdate=MAX_DATE)
# timePicker($Name,$Value,$optional=false)
#
##################################

define('MIN_DATE',date("Y-m-d",strtotime("-5 year")));
define('MAX_DATE',date("Y-m-d",strtotime("+15 year")));

function FileUploadForm($current_file,$current_varname,$new_varname,$file_dir,$size=60) {
    if (!$current_file) {
       return '<input type="file" class="upload" name="'.$new_varname.'" size="'.$size.'" maxlength="255">';
    } else {
       return '<table border="0" cellspacing="2" cellpadding="0" class="text">
                 <tr>
                   <td><input type="radio" name="keep_'.$new_varname.'" value="1" checked> Keep:&nbsp;&nbsp; <a href="'.$file_dir.$current_file.'">'.$current_file.'</a> <input type="hidden" name="'.$current_varname.'" value="'.$current_file.'"></td>
                 </tr>
                 <tr>
                   <td><input type="radio" name="keep_'.$new_varname.'" value="0"> <input type="file" onClick="document.forms[document.forms.length-1].keep_'.$new_varname.'[1].checked=true;" class="upload" name="'.$new_varname.'" size="'.$size.'" maxlength="255"></td>
                 </tr>
               </table>';
    }
}

function PicUploadForm($current_file,$current_varname,$new_varname,$picture_dir,$size=60) {
    // Standard: aus lib_f_pictures
    global $thumbheight,$thumbwidth;
    if (!$current_file) {
       return '<input type="file" class="upload" name="'.$new_varname.'" size="'.$size.'" maxlength="255">';
    } else {
       return '<table border="0" cellspacing="2" cellpadding="0" class="text">
                 <tr>
                   <td><input type="radio" name="keep_'.$new_varname.'" value="1" checked> Keep picture:&nbsp;&nbsp; '.getPic($picture_dir.$current_file,$thumbwidth,$thumbheight).' <input type="hidden" name="'.$current_varname.'" value="'.$current_file.'"></td>
                 </tr>
                 <tr>
                   <td><input type="radio" name="keep_'.$new_varname.'" id="keep_'.$new_varname.'" value="0"> <input type="file" onclick="document.getElementById(\'keep_'.$new_varname.'\').checked=true;" class="upload" name="'.$new_varname.'" size="'.$size.'" maxlength="255"></td>
                 </tr>
               </table>
               <script type="text/javascript">
                    function checknew() {
                      alert("buh");
                      var len = document.forms.length-1;
                      alert(len);
                      alert(document.forms[len].keep_'.$new_varname.');
                    }
               </script>';
    }
}

################ SELECT FROM QUERY ####################################
function selectfromquery($name,$query,$key,$noseltext=false,$onchange='',$size=1) {
      if (($res = mq($query)) || $noseltext) {
          $result = '<select name="'.$name.'" size="'.$size.'" '.(($onchange)? 'onChange="'.$onchange.'"':'').'>'."\n";
          if ($noseltext) $result .= '<option value="0" '.(($key==0)?'selected':'').'>'.$noseltext.'</option>'."\n";
          while ($res && ($data = mysql_fetch_row($res))) {
            $sel = ($data[0] == $key) ? 'selected':'';
            $result .= '<option value="'.$data[0].'" '.$sel.'>'.$data[1].'</option>'."\n";
          }
          return $result.='</select>'."\n";
      } else return false;
}
################ SELECT FROM ARRAY ####################################
function selectfromarray($name,$values,$key,$onchange=false) {
      if (is_array($values)) {
          $result = '<select name="'.$name.'" size="1" '.(($onchange)? 'onChange="'.$onchange.'"':'').'>'."\n";
          foreach ($values as $val => $text) {
            $sel = ($val == $key) ? 'selected':'';
            $result .= '<option value="'.$val.'" '.$sel.'>'.$text.'</option>'."\n";
          }
          return $result.='</select>'."\n";
      } else return false;
}

################ DATE PICKER ####################################
function datePicker($N,$Value,$optional=false,$mindate=MIN_DATE,$maxdate=MAX_DATE) {
  $names = Array('January','February','March','April','May','June','July','August','September','October','November','December');
  if ($Value && $Value != '0000-00-00') {
    if (strtotime($mindate)>strtotime($Value)) {
      $date = explode('-',$mindate);
      $Value = false;
    } else $date = explode('-',$Value);
  } else {
    $date = Array(date("Y"),date("m"),date("d"));
    $Value = ($optional) ? '0000-00-00':((strtotime($mindate)>time()) ? $mindate:now());
  }
  if ($optional) {
    $result = '<input type="checkbox" onClick="'.$N.'_setdate()" name="'.$N.'_isset" '.(($Value != '0000-00-00' && $Value) ? 'checked':'').'> ';
  }
  $mind = explode('-',$mindate); $maxd = explode('-',$maxdate);
  $result .= '<input type="hidden" name="'.$N.'" value="'.$Value.'">
              <select name="'.$N.'_day" size="1" onChange="'.$N.'_check()">';
 # Tageanzeige
  $ds = 1; $de=31; if ($mind[1] == $maxd[1] && $mind[0] == $maxd[0]) {$ds=$mind[2]; $de=$maxd[2];}
  for ($d=$ds;$d<=$de;$d++) {
    $result .= '<option value="'.$d.'"'.(($d == $date[2]) ? ' selected':'').'>'.$d.'</option>';
  }
  $result .= '</select> <b>.</b> <select name="'.$N.'_month" size="1" onChange="'.$N.'_check()">';

 # Monatsanzeige
  $ms = 1; $me = 12; if ($mind[0] == $maxd[0]) {$ms=$mind[1]; $me=$maxd[1];}
  for ($m=$ms;$m <=$me;$m++) {                                                  #$names[date("n",strtotime("+".($m-date("m"))." months"))-1]
    $result .= '<option value="'.$m.'"'.(($m == $date[1]) ? ' selected':'').'>'.$names[$m-1].'</option>';
  }

 # Jahre
  $result .= '</select> <b>.</b> <select name="'.$N.'_year" size="1" onChange="'.$N.'_check()">';
  for ($y = $mind[0];$y <= $maxd[0];$y++) {
    $result .= '<option value="'.$y.'"'.(($y == $date[0]) ? ' selected':'').'>'.$y.'</option>';
  }
  return $result .= '</select>'."\n\n".'
              <script type="text/javascript">
               var '.$N.'_idx = document.forms.length-1;
               var '.$N.'_d = document.forms['.$N.'_idx].'.$N.'_day;
               var '.$N.'_m = document.forms['.$N.'_idx].'.$N.'_month;
               var '.$N.'_Y = document.forms['.$N.'_idx].'.$N.'_year;
               var '.$N.'_optional = '.(($optional) ? 'true':'false').';
               function val(i) {
                 return i.options[i.selectedIndex].value;
               }
               function findItem(item,string) {
                 for (var x = 0; x < item.options.length;x++) {
                   if (item.options[x].value.indexOf(string) > -1) return x;
                 }
                 return 0;
               }
               function '.$N.'_checkVal() {
                 var alerted = false;
                 d = val('.$N.'_d); m = val('.$N.'_m); Y = val('.$N.'_Y);
                // Check Month
                 if (Y == '.$mind[0].' && m < '.$mind[1].') {
                   '.$N.'_m.selectedIndex = findItem('.$N.'_m,'.$mind[1].');
                   alert("Minimales Datum: '.implode('.',array_reverse($mind)).'");
                   alerted = true;
                 } else if (Y == '.$maxd[0].' && m > '.$maxd[1].') {
                   '.$N.'_m.selectedIndex = findItem('.$N.'_m,'.$maxd[1].');
                   alert("Maximales Datum: '.implode('.',array_reverse($maxd)).'");
                   alerted = true;
                 }
                 m = val('.$N.'_m);
                // Check Day
                 if (Y == '.$mind[0].' && m == '.$mind[1].' && d < '.$mind[2].') {
                   '.$N.'_d.selectedIndex = findItem('.$N.'_d,'.$mind[2].');
                   if (!alerted) alert("Minimales Datum: '.implode('.',array_reverse($mind)).'");
                 } else if (Y == '.$maxd[0].' && m == '.$maxd[1].' && d > '.$maxd[2].') {
                   '.$N.'_d.selectedIndex = findItem('.$N.'_d,'.$maxd[2].');
                   if (!alerted) alert("Maximales Datum: '.implode('.',array_reverse($maxd)).'");
                 }
                 d = val('.$N.'_d);
               }
               function '.$N.'_setdate() {
                 '.$N.'_checkVal();
                 var '.$N.'_date= val('.$N.'_Y)+"-"+val('.$N.'_m)+"-"+val('.$N.'_d);
                 if ('.$N.'_optional && !document.forms['.$N.'_idx].elements.'.$N.'_isset.checked) '.$N.'_date="";
                 document.forms['.$N.'_idx].elements.'.$N.'value = '.$N.'_date;
               }
               function '.$N.'_check() {
                 if ('.$N.'_optional) document.forms['.$N.'_idx].elements.'.$N.'_isset.checked = true;
                 '.$N.'_setdate();
               }
              </script>';
}
################ TIME PICKER ####################################
function timePicker($Name,$Value,$optional=false) {
  if ($Value && $Value != '00:00:00') {
    $time = explode(':',$Value);
  } else {
    $time = Array(date("H"),date("i"));
    if (!$optional) $Value = date("H:i");
  }
  if ($optional) {
     $result = '<input onclick="'.$Name.'_settime()" type="checkbox" id="'.$Name.'_isset" name="'.$Name.'_isset"'.(($Value != '00:00:00' && $Value) ? ' checked':'').'> ';
  }
  $result .= '<input type="hidden" name="'.$Name.'" value="'.$Value.'">
              <select name="'.$Name.'_hour" size="1" onChange="'.$Name.'_check()">';
  for ($h=0;$h<24;$h++) {
    $result .= '<option value="'.$h.'" '.(($h == $time[0]) ? 'selected':'').'>'.((strlen($h) == 1) ? '0':'').$h.'</option>';
  }
  $result .= '</select> <b>:</b> <select name="'.$Name.'_min" size="1" onChange="'.$Name.'_check()">';
  for ($m=0;$m <60;$m+=5) {
    $result .= '<option value="'.$m.'" '.((abs($m-$time[1])<5) ? 'selected':'').'>'.((strlen($m) == 1) ? '0':'').$m.'</option>';
  }
  return $result .= '</select> Uhr<script type="text/javascript">
               var '.$Name.'_idx = document.forms.length-1;
               var '.$Name.'_hs = document.forms['.$Name.'_idx].'.$Name.'_hour;
               var '.$Name.'_ms = document.forms['.$Name.'_idx].'.$Name.'_min;
               var '.$Name.'_optional = '.(($optional) ? 'true':'false').';
               function '.$Name.'_settime() {
                 if ('.$Name.'_optional && !document.forms['.$Name.'_idx].elements.'.$Name.'_isset.checked) {
                  var '.$Name.'_time="00:00";
                 } else {
                  var '.$Name.'_time = '.$Name.'_hs.options['.$Name.'_hs.selectedIndex].value+":"+'.$Name.'_ms.options['.$Name.'_ms.selectedIndex].value;
                 }
                 document.forms['.$Name.'_idx].elements.'.$Name.'.value = '.$Name.'_time+":00";
               }
               function '.$Name.'_check() {
                 if ('.$Name.'_optional) document.forms['.$Name.'_idx].elements.'.$Name.'_isset.checked = true;
                 '.$Name.'_settime();
               }
              </script>'."\n\n";
}


?>